import { Schema, model, ObjectId } from "mongoose";

// User Interface
interface User {
  _id: ObjectId;
  email: string;
  password: string;
  username: string;
  gender: string;
  description: string;
  createdAt: Date;
  birthdayAt: Date;
}

// Baseline Interface
interface Baseline {
  _id: ObjectId;
  userId: string;
  height: number;
  weight: number;
  updatedAt: Date;
}

// Health Record Interface
interface HealthRecord {
  _id: ObjectId;
  userId: string;
  recordType: "weight" | "bloodPressure" | "heartRate" | "sleepQuality";
  timestamp: Date;
  details: {
    weight?: {
      value: number;
      unit: "kg" | "lb";
    };
    bloodPressure?: {
      systolic: number;
      diastolic: number;
      pulse?: number;
    };
    heartRate?: {
      current: number;
      max?: number;
      min?: number;
      avg?: number;
    };
    sleepQuality?: {
      totalSleepDuration: number;
      sleepStartTime?: Date;
      wakeUpTime?: Date;
    };
  };
  remark?: string;
  source: "manual" | "device";
  deviceId?: string;
  location?: {
    coordinates: [number, number];
  };
}

// Memo Interface
interface Memo {
  _id: ObjectId;
  userId: string;
  content: string;
  advice?: string;
  mood?: {
    level: 1 | 2 | 3 | 4 | 5;
  };
  createdAt: Date;
  updatedAt: Date;
}

// Mood Interface
interface Mood {
  _id: ObjectId;
  userId: string;
  createdAt: Date;
  mood: {
    level: 1 | 2 | 3 | 4 | 5;
  };
  remark: string;
}

// User Device Interface
interface UserDevice {
  _id: ObjectId;
  userId: string;
  deviceId: string;
  deviceType: string;
  isActive: boolean;
  connectedAt: Date;
}

// Schemas
const userSchema = new Schema<User>({
  email: { type: String, required: true, unique: true, index: true },
  password: { type: String, required: true },
  username: { type: String, required: true, unique: true, index: true },
  gender: { type: String },
  description: { type: String },
  createdAt: { type: Date, default: Date.now },
  birthdayAt: { type: Date },
});

const baselineSchema = new Schema<Baseline>({
  userId: { type: String, required: true, ref: "User" },
  height: { type: Number, required: true },
  weight: { type: Number, required: true },
  updatedAt: { type: Date, default: Date.now },
});

const healthRecordSchema = new Schema<HealthRecord>(
  {
    userId: { type: String, required: true, ref: "User" },
    recordType: {
      type: String,
      required: true,
      enum: ["weight", "bloodPressure", "heartRate", "sleepQuality"],
    },
    timestamp: { type: Date, default: Date.now },
    details: {
      weight: {
        value: Number,
        unit: { type: String, enum: ["kg", "lb"] },
      },
      bloodPressure: {
        systolic: Number,
        diastolic: Number,
        pulse: Number,
      },
      heartRate: {
        current: Number,
        max: Number,
        min: Number,
        avg: Number,
      },
      sleepQuality: {
        totalSleepDuration: Number,
        sleepStartTime: Date,
        wakeUpTime: Date,
      },
    },
    remark: String,
    source: { type: String, required: true, enum: ["manual", "device"] },
    deviceId: String,
    location: {
      coordinates: { type: [Number], index: "2dsphere" },
    },
  },
  { timestamps: true }
);

const memoSchema = new Schema<Memo>(
  {
    userId: { type: String, required: true, ref: "User" },
    content: { type: String, required: true },
    mood: {
      level: { type: Number, enum: [1, 2, 3, 4, 5] },
    },
    advice: String,
  },
  { timestamps: true }
);

const moodSchema = new Schema<Mood>({
  userId: { type: String, required: true, ref: "User" },
  createdAt: { type: Date, default: Date.now },
  mood: {
    level: { type: Number, enum: [1, 2, 3, 4, 5] },
  },
  remark: String,
});

const userDeviceSchema = new Schema<UserDevice>({
  userId: { type: String, required: true, ref: "User" },
  deviceId: { type: String, required: true },
  deviceType: String,
  isActive: { type: Boolean, default: true },
  connectedAt: { type: Date, default: Date.now },
});

// Create compound index for userId and deviceId
userDeviceSchema.index({ userId: 1, deviceId: 1 }, { unique: true });

// Export Models
export const UserModel = model<User>("User", userSchema);
export const BaselineModel = model<Baseline>("Baseline", baselineSchema);
export const HealthRecordModel = model<HealthRecord>(
  "HealthRecord",
  healthRecordSchema
);
export const MemoModel = model<Memo>("Memo", memoSchema);
export const MoodModel = model<Mood>("Mood", moodSchema);
export const UserDeviceModel = model<UserDevice>(
  "UserDevice",
  userDeviceSchema
);

// Export Interfaces
export type { User, Baseline, HealthRecord, Memo, Mood, UserDevice };
